package tree_util;

import com.common.ColonyTool;
import com.common.FishEPTool;
import com.opensymphony.xwork2.ActionSupport;
import db_tool.LogTool;
import org.apache.struts.action.Action;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by 10742 on 2017/7/8.
 */
public class Op_Colony extends ActionSupport {

    private int id;
    private String sampleID;
    private int sampleNum;
    private String description;
    private String dataType;
    private int numSimple;
    private int totalNum;
    private String diversitySimple;
    private String diversityNucle;
    private int numAllele;
    private String observeHeter;
    private String expectHeter;
    private String flag;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getSampleID() {
        return sampleID;
    }

    public void setSampleID(String sampleID) {
        this.sampleID = sampleID;
    }

    public int getSampleNum() {
        return sampleNum;
    }

    public void setSampleNum(int sampleNum) {
        this.sampleNum = sampleNum;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDataType() {
        return dataType;
    }

    public void setDataType(String dataType) {
        this.dataType = dataType;
    }

    public int getNumSimple() {
        return numSimple;
    }

    public void setNumSimple(int numSimple) {
        this.numSimple = numSimple;
    }

    public int getTotalNum() {
        return totalNum;
    }

    public void setTotalNum(int totalNum) {
        this.totalNum = totalNum;
    }

    public String getDiversitySimple() {
        return diversitySimple;
    }

    public void setDiversitySimple(String diversitySimple) {
        this.diversitySimple = diversitySimple;
    }

    public String getDiversityNucle() {
        return diversityNucle;
    }

    public void setDiversityNucle(String diversityNucle) {
        this.diversityNucle = diversityNucle;
    }

    public int getNumAllele() {
        return numAllele;
    }

    public void setNumAllele(int numAllele) {
        this.numAllele = numAllele;
    }

    public String getObserveHeter() {
        return observeHeter;
    }

    public void setObserveHeter(String observeHeter) {
        this.observeHeter = observeHeter;
    }

    public String getExpectHeter() {
        return expectHeter;
    }

    public void setExpectHeter(String expectHeter) {
        this.expectHeter = expectHeter;
    }

    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

    @Override
    public String execute() throws IOException {
        ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
        ColonyTool tool = (ColonyTool) context.getBean("colonyTool");
        ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
        PrintWriter writer = ServletActionContext.getResponse().getWriter();
        System.out.println(flag);

        if (flag.equals("insert")) {
            LogTool.insertIntoDataLog(LogTool.INSERT_CODE, "Colony_ResearData", 1);
            id =  tool.insert(sampleID, sampleNum, description, dataType, numSimple, totalNum, diversitySimple, diversityNucle, numAllele, observeHeter, expectHeter);
            LogTool.insertIntoDataLog(LogTool.INSERT_CODE, "Colony_ResearData", 1);
            writer.write(String.valueOf(id));
            writer.close();
        } else if (flag.equals("update")) {
            LogTool.insertIntoDataLog(LogTool.UPDATE_CODE, "Colony_ResearData", 1);
            System.out.println(id + " " +sampleNum+description+dataType+numSimple+totalNum+diversitySimple+diversityNucle+numAllele+observeHeter+expectHeter);
            int column = tool.update(id, sampleNum, description, dataType, numSimple, totalNum, diversitySimple, diversityNucle, numAllele, observeHeter, expectHeter);
            if (column == 1){
                writer.write("true");
                LogTool.insertIntoDataLog(LogTool.UPDATE_CODE, "Colony_ResearData", 1);
            }
            writer.close();
        }
        return null;
    }

}
